package com.cuitrip.component.popupmenu;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.cuitrip.app.util.FontManager;
import com.cuitrip.service.R;
import com.cuitrip.util.UIUtil;
import com.lab.adapter.HolderViewAdapter;

import java.util.List;

/**
 * Created by ak
 */
public class PopupMenu {

    private Context mContext;
    private List<PopupMenuItem> mPopupMenuItemList;
    private PopupMenuCallback mPopupMenuCallback;

    private PopupWindow mPopupWindow;
    private ViewGroup mRootView;
    private ViewGroup mPopupMenuRootView;
    private ListView mPopupMenuListView;

    private HolderViewAdapter mPopupMenuItemAdapter;

    public static PopupMenu getInstance(Context context, List<PopupMenuItem> popupMenuItemList, PopupMenuCallback popupMenuCallback) {
        if (null == context || null == popupMenuItemList || popupMenuItemList.size() <= 0)
            return null;
        // 设置最后一个PopupItem不需要BottomLine
        int popopMenuListSize = popupMenuItemList.size();
        if (popopMenuListSize > 0) {
            PopupMenuItem lastPopupMenuItem = popupMenuItemList.get(popopMenuListSize - 1);
            if (null != lastPopupMenuItem) {
                lastPopupMenuItem.setNeedBottomLine(false);
            }
        }
        PopupMenu popupMenu = new PopupMenu(context);
        popupMenu.setPopupMenuItemList(popupMenuItemList);
        popupMenu.setPopupMenuCallback(popupMenuCallback);
        popupMenu.internalInit();
        return popupMenu;
    }

    private PopupMenu(Context context) {
        mContext = context;
    }

    private void internalInit() {
        // 初始化UI控件
        mRootView = (ViewGroup) LayoutInflater.from(mContext).inflate(R.layout.popup_menu_layout, null, false);
        FontManager.resetViewFont(mRootView,true);
        mPopupMenuRootView = UIUtil.findViewById(mRootView, R.id.popup_menu_root, ViewGroup.class);
        mPopupMenuListView = UIUtil.findViewById(mRootView, R.id.popup_menu_list, ListView.class);
        mPopupMenuRootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hide();
            }
        });
        // 设置ListView及Adapter
        mPopupMenuItemAdapter = new HolderViewAdapter(mContext, mPopupMenuItemList, PopupMenuItemHolderView.class);
        mPopupMenuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 是否维持PopupMenu
                boolean holdPopupMenu = false;
                if (null != mPopupMenuCallback) {
                    holdPopupMenu = mPopupMenuCallback.onPopupMenuItemClick(position,mPopupMenuItemList.get(position));
                }
                if (!holdPopupMenu)
                    hide();
            }
        });
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mPopupMenuListView.getLayoutParams();
        lp.width = RelativeLayout.LayoutParams.MATCH_PARENT;
        lp.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
        mPopupMenuListView.setLayoutParams(lp);
        mPopupMenuListView.setAdapter(mPopupMenuItemAdapter);
        // 初始化PopupWindow
        mPopupWindow = new PopupWindow(mRootView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                if (mPopupMenuCallback != null){
                    mPopupMenuCallback.onPopupMenuStateChanged(false);
                }
            }
        });
    }
    //设置popupMenu显示位置
    public void show(View anchorView) {
        if (null != anchorView) {
            mPopupWindow.showAsDropDown(anchorView, 0, 0);
            if (mPopupMenuCallback != null){
                mPopupMenuCallback.onPopupMenuStateChanged(true);
            }
        }
    }

    public void hide() {
        mPopupWindow.dismiss();
    }

    private void setPopupMenuItemList(List<PopupMenuItem> popupMenuItemList) {
        mPopupMenuItemList = popupMenuItemList;
    }

    private void setPopupMenuCallback(PopupMenuCallback callback) {
        mPopupMenuCallback = callback;
    }

    public interface PopupMenuCallback {
        boolean onPopupMenuItemClick(int position, PopupMenuItem popupMenuItem);

        void onPopupMenuStateChanged(boolean isShow);
    }

}
